package com.smart.upms.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.utils.CopyUtils;
import com.smart.upms.sys.entity.SysLog;
import com.smart.upms.sys.mapper.SysLogMapper;
import com.smart.upms.sys.param.SysLogParam;
import com.smart.upms.sys.param.SysLogQueryParam;
import com.smart.upms.sys.result.SysLogResult;
import com.smart.upms.sys.service.ISysLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Service
@AllArgsConstructor
public class SysLogServiceImpl implements ISysLogService {

    private final SysLogMapper mapper;


    @Override
    public Boolean save(SysLogParam param) {
        SysLog entity = CopyUtils.copyObject(param, SysLog.class);
        int insert = mapper.insert(entity);
        return insert > 0;
    }

    @Override
    public Boolean update(SysLogParam param) {
        SysLog entity = CopyUtils.copyObject(param, SysLog.class);
        int update = mapper.updateById(entity);
        return update > 0;

    }

    @Override
    public Boolean delete(Long id) {
        int delete = mapper.deleteById(id);
        return delete > 0;
    }

    @Override
    public SysLogResult get(Long id) {
        SysLog entity = mapper.selectById(id);
        return CopyUtils.copyObject(entity, SysLogResult.class);
    }

    @Override
    public List<SysLogResult> list(SysLogQueryParam param) {
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        List<SysLog> entityList = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(entityList, SysLogResult.class);
    }

    @Override
    public Page<SysLogResult> listPage(Page<SysLogResult> page, SysLogQueryParam param) {
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        if (param.getLogType() != null) {
            queryWrapper.eq(SysLog.LOG_TYPE, param.getLogType());
        }
        if (param.getServiceId() != null) {
            queryWrapper.eq(SysLog.SERVICE_ID, param.getServiceId());
        }
        if (param.getTitle() != null) {
            queryWrapper.eq(SysLog.TITLE, param.getTitle());
        }
        Page<SysLog> entityPage = new Page<>();
        entityPage.setSize(page.getSize());
        entityPage.setCurrent(page.getCurrent());
        entityPage.setAsc(page.ascs());
        entityPage.setDesc(page.descs());
        mapper.selectPage(entityPage, queryWrapper);
        Page<SysLogResult> resultPage = CopyUtils.copyPage(entityPage, SysLogResult.class);
        return resultPage;
    }


}
